package com.lrh.java8;

import javax.naming.Name;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Arrays;
import java.util.List;
import java.util.Random;
import java.util.function.Supplier;
import java.util.stream.Stream;

public class CreateStream {

    public static void main(String[] args) {
        createStreamFromCollection().forEach(System.out::println);
        createStreamFromValues().forEach(System.out::println);
        createStreamFromArrays().forEach(System.out::println);
        System.out.println("==================");
        createStreamFromFile();
        createStreamFromIterator().forEach(System.out::println);
        createStreamFromGenerate().forEach(System.out::println);
        System.out.println("==================");
        createObjStreamFromGenerate().forEach(System.out::println);
    }
    private  static Stream<String> createStreamFromCollection(){
        List<String> list = Arrays.asList("hello", "alex", "wangwen", "world");
        return list.stream();
    }

    private static Stream<String> createStreamFromValues(){
        return Stream.of("hello", "alex", "wangwen", "world");
    }

    private static Stream<String> createStreamFromArrays(){
        return Arrays.stream(new String[]{"hello", "alex", "wangwen", "world"});
    }

    private static Stream<String> createStreamFromFile(){
        Path path = Paths.get("D:\\JAVA\\ideaworkspace\\java8\\java8-sharing\\src\\main\\java\\com\\lrh\\java8\\CreateStream.java");
        try(Stream<String> lines = Files.lines(path)) {
            System.out.println("=====================");
            lines.forEach(System.out::println);
            return lines;
        }catch (IOException e){
            throw new RuntimeException(e);
        }
    }

    public static Stream<Integer> createStreamFromIterator(){
        Stream<Integer> stream = Stream.iterate(0, n -> n+2).limit(10);
        return stream;
    }

    public static Stream<Double> createStreamFromGenerate(){
        Stream<Double> stream = Stream.generate(Math::random).limit(10);
        return stream;
    }

    public static Stream<Obj> createObjStreamFromGenerate(){
        return Stream.generate(new ObjSupplier()).limit(10);
    }

    static class ObjSupplier implements Supplier<Obj> {
        private int index = 0;
        private Random random = new Random(System.currentTimeMillis());

        @Override
        public Obj get() {
            index = random.nextInt(100);
            return new Obj(index, "Name->" + index);
        }
    }

    public static class Obj{
        private int id;

        private String name;

        public Obj(int id, String name) {
            this.id = id;
            this.name = name;
        }

        public int getId() {
            return id;
        }

        public void setId(int id) {
            this.id = id;
        }

        public String getName() {
            return name;
        }

        public void setName(String name) {
            this.name = name;
        }
    }
}
